import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/home',
    redirect: '/'
  },
  {
    path: '/cart',
    name: 'cart',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "cart" */ '../views/Cart.vue')
  },
  {
    path: '/recipe',
    name: 'recipe',
    component: () =>
      import(/* webpackChunkName: "recipe" */ '../views/Recipe.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue')
  },
  {
    path: '/cate',
    name: 'cate',
    component: () => import(/* webpachChunkName: "cate" */ '../views/Cate.vue')
  },
  {
    path: '/all_product',
    name: 'allProduct',
    component: () =>
      import(/* webpachChunkName: "all_product" */ '../views/AllProduct.vue')
  },
  {
    path: '/product_detail/:product_id',
    name: 'productDetail',
    component: () =>
      import(
        /* webpachChunkName: "product_detail" */ '../views/ProductDetail.vue'
      )
  },
  {
    path: '/article/:recipe_id',
    name: 'article',
    component: () =>
      import(/* webpackChunkName: "article" */ '../views/RecipeArticle.vue')
  },
  {
    path: '/orders',
    name: 'orders',
    component: () =>
      import(/* webpackChunkName: "orders" */ '../views/Orders.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () =>
      import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/forget',
    name: 'forget',
    component: () =>
      import(/* webpackChunkName: "forget" */ '../views/Forget.vue')
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // if (to.path === '/login') {
  //   return next()
  // }
  // const token = window.localStorage.getItem('token') || ''
  // if (!token) {
  //   next('/login')
  // } else {
  //   next()
  // }
  next()
})

export default router
